<!-- 实体类数据 -->
<template>
  <div class="type-tree">
    <div class="page-title">
      <span class="page-title-text">实体类别</span>
      <div></div>
    </div>
    <div class="tree-search">
      <el-input
        v-model="filterText"
        size="small"
        placeholder="请输入过滤关键词"
        @input="filterTree"
        style="margin-bottom: 10px"
      ></el-input>
    </div>
    <div class="tree-container">
      <el-tree
        ref="treeRef"
        class="tree-list filter-tree tree-scrollbar"
        node-key="value"
        :data="useTreeData"
        :props="defaultProps"
        draggable
        :default-expand-all="true"
        render-after-expand
        auto-expand-parent
        :highlight-current="true"
        :filter-node-method="filterMethod"
        :expand-on-click-node="false"
        @node-click="handleNodeClick"
      >
      </el-tree>
    </div>
  </div>
</template>
<script>
export default {
  name: "EntityTypeTree",
  props: {
    treeData: Array,
    default: () => [],
  },
  computed: {
    useTreeData() {
      return [
        {
          label: "全部",
          value: null,
          children: this.treeData,
        },
      ];
    },
  },
  data() {
    return {
      filterText: null,
      defaultProps: {
        label: "label",
        children: "children",
      },
      // 当前选择节点的详细数据
      curItem: {},
    };
  },
  methods: {
    filterTree(val) {
      this.$refs.treeRef.filter(val);
    },
    // 过滤方法
    filterMethod(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 点击节点
    handleNodeClick(data) {
      this.curItem = data;
      this.$emit("select-node", this.curItem);
    },
  },
};
</script>
<style lang="scss" scoped>
.type-tree {
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
  height: 100%;
  padding: 20px;
  background-color: #fff;
}
.page-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
  .page-title-text {
    border-left: 5px solid $--color-primary;
    padding-left: 10px;
    margin: 8px 0;
    font-size: 16px;
    line-height: 16px;
  }
}

.tree-container {
  flex: 1;
  padding: 10px 0;
  overflow-y: auto;
}

.custom-tree-node {
  flex: 1;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  .node-left {
    flex: 1;
    overflow: hidden;
    display: flex;
    align-items: center;
    .node-label {
      flex: 1;
      overflow: hidden;
      text-overflow: ellipsis;
    }
  }
  .node-icon {
    margin-right: 6px;
    box-sizing: border-box;
    display: flex;
    align-items: center;
  }
  .node-right {
    opacity: 0;
    margin: 0 10px;
  }
  &:hover .node-right {
    opacity: 1;
  }
  &:hover span {
    color: $--color-primary;
  }
}
</style>
